using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Microsoft.Phone.Controls.Primitives
{
	[TemplateVisualState(Name = "Unselected", GroupName = "SelectionStates"), TemplateVisualState(Name = "Selected", GroupName = "SelectionStates")]
	public class PivotHeaderItem : ContentControl
	{
		private const string SelectedState = "Selected";
		private const string UnselectedState = "Unselected";
		private const string SelectionStatesGroup = "SelectionStates";
		public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.Register("IsSelected", typeof(bool), typeof(PivotHeaderItem), new PropertyMetadata(false, new PropertyChangedCallback(PivotHeaderItem.OnIsSelectedPropertyChanged)));
		public bool IsSelected
		{
			get
			{
				return (bool)base.GetValue(PivotHeaderItem.IsSelectedProperty);
			}
			set
			{
				base.SetValue(PivotHeaderItem.IsSelectedProperty, value);
			}
		}
		internal PivotHeadersControl ParentHeadersControl
		{
			get;
			set;
		}
		internal object Item
		{
			get;
			set;
		}
		private static void OnIsSelectedPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			PivotHeaderItem pivotHeaderItem = d as PivotHeaderItem;
			if (pivotHeaderItem.ParentHeadersControl != null)
			{
				pivotHeaderItem.ParentHeadersControl.NotifyHeaderItemSelected(pivotHeaderItem, (bool)e.get_NewValue());
				pivotHeaderItem.UpdateVisualStates(true);
			}
		}
		public PivotHeaderItem()
		{
			base.set_DefaultStyleKey(typeof(PivotHeaderItem));
		}
		protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
		{
			base.OnMouseLeftButtonUp(e);
			if (e != null && !e.get_Handled())
			{
				e.set_Handled(true);
				if (this.ParentHeadersControl != null)
				{
					this.ParentHeadersControl.OnHeaderItemClicked(this);
				}
			}
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.UpdateVisualStates(false);
		}
		internal void UpdateVisualStateToUnselected()
		{
			VisualStateManager.GoToState(this, "Unselected", false);
		}
		internal void RestoreVisualStates()
		{
			this.UpdateVisualStates(false);
		}
		private void UpdateVisualStates(bool useTransitions)
		{
			VisualStateManager.GoToState(this, this.IsSelected ? "Selected" : "Unselected", useTransitions);
		}
	}
}
